<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>文件上传测试</h1>
<!--如果需要上传文件 必须设置编码类型为多部分数据-->
<form action="" enctype="multipart/form-data">
    <input type="text" name="title" placeholder="标题">
    <input type="file" name="picFile" >
    <input type="button" value="上传" @click="upload()">
    <hr>
    <div v-for="image in arr">
        <h3>{{image.title}}</h3>
        <img :src="image.url" style="width: 200px" alt="">
        <a href="#" @click="del(image.id)">删除</a>
    </div>
</form>
<!--引入vue框架-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v=new Vue({
        el:"form",
        data:{
            arr:[]
        },

        methods:{
            upload:function () {
                //得到表单里面的数据
                let data=new FormData(document.querySelector("form"))
                axios.post("/upload",data).then(function () {
                    alert("上传完成")
                    location.reload();
                })
            },
            del:function (id) {
                if (confirm("是否删除")){
                axios.get("/delete?id="+id).then(function (response) {
                    alert("删除成功")
                    location.reload();
                })}
            }
        },
        created:function () {
            //获取请求获取所有图片信息
            axios.get("/select").then(function (response) {
                v.arr=response.data;


            })
        }

    })
</script>
</body>
</html>